/*
 * This file is part of Jeve.

    Jeve is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Jeve is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Copyright 2007 Jochen Bedersdorfer (beders@yahoo.com)
 *
 */
package de.monoid.eve.cmd;

import java.io.FileInputStream;
import java.util.Date;
import java.util.Properties;

import de.monoid.eve.APIKey;
import de.monoid.eve.Character;
import de.monoid.eve.DataStore;
import de.monoid.eve.UserID;
import de.monoid.eve.notifier.Notifier;

/**
 * Simple reminder of finished skills.
 * To notify an user, you can plugin in different providers for interface Notifier.
 * 
 * @author beders
 *
 */
public class NotifySkill {
 
  /**
   * @param args
   * @throws Exception  
   */
  public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    if (args.length != 1) {
      System.out.println("Usage: java -jar jeve.jar de.monoid.eve.cmd.NotifySkill <property-file>");
      System.out.println("Specify the location of a property file!");
      System.exit(1);
    }
    new NotifySkill().execute(args[0]); 
  }

  private void execute(String aPropFile) throws Exception {
    Properties p = new Properties();
    p.load(new FileInputStream(aPropFile));
    UserID id = new UserID(p.getProperty("eve.userid"));
    APIKey key = new APIKey(p.getProperty("eve.apikey"));
    DataStore ds = new DataStore(id,key);
    Character ch = ds.getCharacter(p.getProperty("eve.char-id"));
    long remaining = ch.getRemainingTrainingTime();
    if (remaining <=0) {
      System.out.println("Training finished or data not available!");
      return;
    }
    notify(remaining, p);    
  }

  /**
   * Notify about the skill training
   * @param remainingTime
   * @param p
   * @throws Exception
   */
  protected void notify(long remainingTime, Properties p) throws Exception {
    String notifierClass = p.getProperty("jeve.notifier", de.monoid.eve.notifier.PrintNotifier.class.getName());
    Notifier notifier = Class.forName(notifierClass).asSubclass(Notifier.class).newInstance();    
    String message = "Skill training has ended!";
    Date notificationTime = new Date(System.currentTimeMillis() + remainingTime);
    notifier.notify(notificationTime, message, p);
  }

}
